//
//  HUDHelper.m
//
//
//  Created by Alexi on 12-11-28.
//  Copyright (c) 2012年 . All rights reserved.
//

#import "HUDHelper.h"
#import "NSString+ACCommon.h"
#import "UIAlertView+BlocksKit.h"

@implementation HUDHelper

static HUDHelper *_instance = nil;


+ (HUDHelper *)sharedInstance
{
    @synchronized(_instance)
    {
        if (_instance == nil)
        {
            _instance = [[HUDHelper alloc] init];
        }
        return _instance;
    }
}

- (MBProgressHUD *)loading
{
    return [self loading:nil];
}

- (MBProgressHUD *)loading:(NSString *)msg
{
    return [self loading:msg inView:nil];
}

- (MBProgressHUD *)loading:(NSString *)msg inView:(UIView *)view
{
    UIView *inView = view ? view : [UIApplication sharedApplication].keyWindow;
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:inView];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        if (![NSString isEmpty:msg])
        {
            hud.mode = MBProgressHUDModeIndeterminate;
            //            hud.label.text = msg;
            hud.labelText=msg;
        }else{
            hud.mode = MBProgressHUDModeCustomView;
            NSMutableArray * images = [NSMutableArray array];
            for (int index = 1; index <= 8; index++) {
                NSString * imageName = [NSString stringWithFormat:@"refresh_0%d.png",index];
                UIImage *image = [UIImage imageNamed:imageName];
                if (image != nil) {
                    [images addObject:image];
                }
            }
            UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"refresh_01"]];
            imgView.animationImages = images;
            imgView.animationDuration = 1.0f;
            imgView.animationRepeatCount = 0;
            [imgView startAnimating];
            hud.customView = imgView;
            hud.color = [UIColor clearColor];
        }
        [inView addSubview:hud];
        [hud show:YES];
        // 超时自动消失
        // [hud hide:YES afterDelay:kRequestTimeOutTime];
    });
    return hud;
}

- (void)loading:(NSString *)msg delay:(CGFloat)seconds execute:(void (^)(void))exec completion:(void (^)(void))completion
{
    dispatch_async(dispatch_get_main_queue(), ^{
        UIView *inView = [UIApplication sharedApplication].keyWindow;
        MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:inView];
        if (![NSString isEmpty:msg])
        {
            hud.mode = MBProgressHUDModeText;
            //            hud.label.text = msg;
            hud.labelText=msg;
        }
        
        [inView addSubview:hud];
        [hud show:YES];
        if (exec)
        {
            exec();
        }
        
        // 超时自动消失
        [hud hide:YES afterDelay:seconds];
        
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(seconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if (completion)
            {
                completion();
            }
        });
    });
}


- (void)stopLoading:(MBProgressHUD *)hud
{
    [self stopLoading:hud message:nil];
}

- (void)stopLoading:(MBProgressHUD *)hud message:(NSString *)msg
{
    [self stopLoading:hud message:msg delay:0 completion:nil];
}
- (void)stopLoading:(MBProgressHUD *)hud message:(NSString *)msg delay:(CGFloat)seconds completion:(void (^)(void))completion
{
    if (hud && hud.superview)
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (![NSString isEmpty:msg])
            {
                //                hud.label.text = msg;
                hud.labelText=msg;
                hud.mode = MBProgressHUDModeText;
            }
            
            [hud hide:YES afterDelay:seconds];
            _syncHUD = nil;
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(seconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                if (completion)
                {
                    completion();
                }
            });
        });
    }
    
}


- (void)tipMessage:(NSString *)msg
{
    [self tipMessage:msg delay:2];
}

- (void)tipMessage:(NSString *)msg delay:(CGFloat)seconds
{
    [self tipMessage:msg delay:seconds completion:nil];
    
}

- (void)tipMessage:(NSString *)msg delay:(CGFloat)seconds completion:(void (^)(void))completion
{
    if ([NSString isEmpty:msg])
    {
        return;
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:[UIApplication sharedApplication].keyWindow];
        [[UIApplication sharedApplication].keyWindow addSubview:hud];
        hud.mode = MBProgressHUDModeText;
        //        hud.label.text = msg;
        hud.labelText=msg;
        [hud show:YES];
        [hud hide:YES afterDelay:seconds];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(seconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if (completion)
            {
                completion();
            }
        });
    });
}


#define kSyncHUDStartTag  100000

// 网络请求
- (void)syncLoading
{
    [self syncLoading:nil];
}
- (void)syncLoading:(NSString *)msg
{
    [self syncLoading:msg inView:nil];
}
- (void)syncLoading:(NSString *)msg inView:(UIView *)view
{
    if (_syncHUD)
    {
        _syncHUD.tag++;
        
        if (![NSString isEmpty:msg])
        {
            //            _syncHUD.label.text = msg;
            _syncHUD.labelText=msg;
            _syncHUD.mode = MBProgressHUDModeText;
        }
        else
        {
            //            _syncHUD.label.text = nil;
            _syncHUD.labelText=nil;
            _syncHUD.mode = MBProgressHUDModeIndeterminate;
        }
        
        return;
    }
    _syncHUD = [self loading:msg inView:view];
    _syncHUD.tag = kSyncHUDStartTag;
}

- (void)syncStopLoading
{
    [self syncStopLoadingMessage:nil delay:0 completion:nil];
}
- (void)syncStopLoadingMessage:(NSString *)msg
{
    [self syncStopLoadingMessage:msg delay:1 completion:nil];
}
- (void)syncStopLoadingMessage:(NSString *)msg delay:(CGFloat)seconds completion:(void (^)(void))completion
{
    _syncHUD.tag--;
    if (_syncHUD.tag > kSyncHUDStartTag)
    {
        if (![NSString isEmpty:msg])
        {
            //            _syncHUD.label.text = msg;
            _syncHUD.labelText=msg;
            _syncHUD.mode = MBProgressHUDModeText;
        }
        else
        {
            //            _syncHUD.label.text = nil;
            _syncHUD.labelText=nil;
            _syncHUD.mode = MBProgressHUDModeIndeterminate;
        }
        
    }
    else
    {
        [self stopLoading:_syncHUD message:msg delay:seconds completion:completion];
    }
}



@end

